<?php

namespace app\api\controller;

use app\admin\model\Memberlevel;
use app\api\service\Member as MemberService;
use app\api\service\Message as MessageService;

use think\Controller;
use think\Request;

class Base extends Controller
{

    const MSG_SUCCESS = '请求成功';
    const MSG_ERROR = '请求失败';
    const NOT_DATA = '暂无数据';
    const NOT_PARAM = '缺少参数';


    public function getHttpParam()
    {
        $param = json_encode(Request::instance()->param());
        return json_decode($param);
    }

    /**
     * 获取用户详细信息
     */
    protected function getMemberInfo($member){
        if(!$member){
            return $member;
        }

        // 登录后执行操作
        MemberService::afterLogin($member['id']);

        // 获取会员等级
        $levelMdl = new Memberlevel();
        $member_level_id = $levelMdl->where('id',$member['member_level_id'])->find();
        $member['member_level_name'] = $member_level_id['name'];
        // 会员等级开通时间-时间戳
        $member['level_open_time_stamp'] = strtotime($member['level_open_time']);
        // 会员等级结束时间-时间戳
        $member['level_end_time_stamp'] = strtotime($member['level_end_time']);

        // 购物车信息
        $cartModel = new \app\admin\model\Shoppingcart();
        $member['shoppingcartData'] = $cartModel->getCurrentCartInfo($member->id);

        // 获取优惠券信息
        $couponMdl = new \app\admin\model\Membercoupon();
        $time = date('Y-m-d H:i:s',time());
        $couponWhere = [
            'a.member_id' => $member['id'],
            'c.end_time' => ['gt',$time],
            'a.status'   => 1
        ];

        $member['couponCount'] = $couponMdl->alias('a')
            ->join('coupon c','a.coupon_id = c.id')
            ->where($couponWhere)->count();
        $member['open_time'] = substr($member['open_time'],0,10);
        $member['end_time'] = substr($member['end_time'],0,10);
        $member['birthday'] = substr($member['birthday'],0,10);
        //如果生日时间未填写 则返回null
        if (empty($member['birthday'])){
            $member['birthday'] = null;
        }

        // 未读消息数
        $unreadMessages = MessageService::getUnreadMessages($member['id']);
        $member['unreadMessages'] = $unreadMessages;

        return $member;
    }

}